package com.ibigcat.module.mdm.service.impl;

import com.ibigcat.module.mdm.service.OperatorLogService;
import org.springframework.stereotype.Service;
import jakarta.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;
import com.ibigcat.module.mdm.controller.admin.operatorlog.vo.*;
import com.ibigcat.module.mdm.dao.entity.OperatorLogDO;
import com.ibigcat.framework.common.pojo.PageResult;
import com.ibigcat.framework.common.pojo.PageParam;
import com.ibigcat.framework.common.util.object.BeanUtils;

import com.ibigcat.module.mdm.dao.mapper.OperatorLogMapper;

import static com.ibigcat.framework.common.exception.util.ServiceExceptionUtil.exception;
import static com.ibigcat.module.mdm.enums.ErrorCodeConstants.*;

/**
 * 操作日志 Service 实现类
 *
 * @author 芋道源码
 */
@Service
@Validated
public class OperatorLogServiceImpl implements OperatorLogService {

    @Resource
    private OperatorLogMapper operatorLogMapper;

    @Override
    public Long createOperatorLog(OperatorLogSaveReqVO createReqVO) {
        // 插入
        OperatorLogDO operatorLog = BeanUtils.toBean(createReqVO, OperatorLogDO.class);
        operatorLogMapper.insert(operatorLog);
        // 返回
        return operatorLog.getId();
    }

    @Override
    public void updateOperatorLog(OperatorLogSaveReqVO updateReqVO) {
        // 校验存在
        validateOperatorLogExists(updateReqVO.getId());
        // 更新
        OperatorLogDO updateObj = BeanUtils.toBean(updateReqVO, OperatorLogDO.class);
        operatorLogMapper.updateById(updateObj);
    }

    @Override
    public void deleteOperatorLog(Long id) {
        // 校验存在
        validateOperatorLogExists(id);
        // 删除
        operatorLogMapper.deleteById(id);
    }

    private void validateOperatorLogExists(Long id) {
        if (operatorLogMapper.selectById(id) == null) {
            throw exception(OPERATOR_LOG_NOT_EXISTS);
        }
    }

    @Override
    public OperatorLogDO getOperatorLog(Long id) {
        return operatorLogMapper.selectById(id);
    }

    @Override
    public PageResult<OperatorLogDO> getOperatorLogPage(OperatorLogPageReqVO pageReqVO) {
        return operatorLogMapper.selectPage(pageReqVO);
    }

}